package com.zhike.b2b.buy.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.zhike.b2b.Constants;
import com.zhike.b2b.base.BaseService;
import com.zhike.b2b.buy.IBuyService;
import com.zhike.b2b.company.ICompanyService;
import com.zhike.b2b.html.ILinkService;
import com.zhike.b2b.model.BuyClassVo;
import com.zhike.b2b.model.BuyVo;
import com.zhike.b2b.model.ClassVo;
import com.zhike.b2b.model.CompanyVo;
import com.zhike.b2b.model.LinkVo;
import com.zhike.b2b.model.ProductClassVo;
import com.zhike.b2b.model.ProductVo;
import com.zhike.b2b.model.UserInfo;
import com.zhike.b2b.util.ClassUtil;
import com.zhike.b2b.util.PageTool;

@Service
public class BuyService extends BaseService implements IBuyService {
	@Resource
	public ICompanyService cs;
	@Resource
	public ILinkService ls;
	 /**
	  * 获取最新的求购信息
	  */
	 public List<BuyVo> getNewestBuyList(int start, int size)
	  {
		  String sql = " select b.buying_name,b.buying_id  buying_id  ,b.class_id class_id,BUYING_NAME name,publish_date,sc.article_rule,sc.list_rule,sc.store_path store_path from buying b   left join buying_class as sc on sc.class_id = b.class_id  where b.start_date <=now() and b.end_date >=now() and b.display=1 and b.validity =1  order by  b.oper_time desc ";
		  sql += " limit "+start+","+size ;
		  List<BuyVo> lnc = jt.query(sql, new RowMapper<BuyVo>() {
			  public BuyVo mapRow(ResultSet rs, int rowNum) throws SQLException {
					BuyVo bv = new BuyVo();
					bv.setClassId(rs.getString("class_id"));
					bv.setBuyId(rs.getString("buying_id"));
					bv.setListRule(rs.getString("list_rule"));
					bv.setStorePath(rs.getString("store_path"));
					bv.setArticleRule(rs.getString("article_rule"));
					bv.setBuyName(rs.getString("buying_name"));
					bv.setPublishTime(rs.getString("publish_date"));
					bv.setUrl(ClassUtil.getObjUrl(bv, bv.getBuyId()));
					return bv;
		      }
		  });
		  return lnc;
	  }
	
	/**
	 * 根据类别ID查询求购信息类别
	 * @param classId
	 * @return
	 */
	public ClassVo getClassById(String classId)
	{
		String sql = "select class_id,class_name,list_rule,home_name,store_path,index_model ,list_model from buying_class where class_id = ? ";
		return jt.queryForObject(sql,  new RowMapper<BuyClassVo>() {
			 public BuyClassVo mapRow(ResultSet rs, int rowNum)
				throws SQLException {
				 BuyClassVo ncv = new BuyClassVo();
				 ncv.setClassId(rs.getString("class_id"));
				 ncv.setListRule(rs.getString("list_rule"));
				 ncv.setStorePath(rs.getString("store_path"));
				 ncv.setHomeName(rs.getString("home_name"));
				 ncv.setListModel(rs.getString("list_model"));
				 ncv.setIndexModel(rs.getString("index_model"));
				 ncv.setClassName(rs.getString("class_name"));
				 return ncv;
			}}, classId);
	}

	/**
	 * 根据条件查询求购信息类别列表
	 * @param clzLevel
	 * @param limitStr
	 * @param orderCln
	 * @param commend
	 * @return
	 */
	public List<ClassVo> queryBuyClzs(String clzId,String limitStr,final boolean isRecursion)
	{
		List<ClassVo> lpcv = jt.query(getSqlstr(clzId,limitStr,"","",""),  new RowMapper<ClassVo>() {
			 public BuyClassVo mapRow(ResultSet rs, int rowNum)
				throws SQLException {
				 BuyClassVo pcv = new BuyClassVo();
				 pcv.setClassId(rs.getString("class_id"));
				 pcv.setListRule(rs.getString("list_rule"));
				 pcv.setStorePath(rs.getString("store_path"));
				 pcv.setHomeName(rs.getString("home_name"));
				 pcv.setClassName(rs.getString("class_name"));
				 pcv.setDataCount(rs.getInt("datacount"));
				 pcv.setListUrl(ClassUtil.getListUrl(pcv, 1));
				 if(isRecursion && pcv.getClassId().length()<10)
					 pcv.setSubClzs(queryBuyClzs(pcv.getClassId(),"",isRecursion));
				 return pcv;
			}});
		return lpcv;
	}
	/**
	 * 根据条件查询求购信息列表
	 * 
	 * @param classId
	 * @param limitStr
	 * @param orderCln
	 * @param commend
	 * @return
	 */
	public List<BuyVo> queryBuys(String classId,String commend ,String limitStr,
			String orderCln) {
		final String defaultLogoImg = ps.getParamValue("default_img");
       StringBuffer sql = new StringBuffer("select distinct b.buying_name,b.buying_id,b.attach_id,a.file_path buy_photo,b.class_id,bc.list_rule,bc.store_path,bc.article_rule, ");
	   
       sql.append("b.BUYING_UNIT BUYING_UNIT,b.EXPECT_PRICES price,b.MONEYTYPE from buying b left join attach a on b.attach_id = a.attach_root_id inner join buying_class bc on bc.class_id=b.class_id where b.start_date <=now() and b.end_date >=now() and b.display=1 and b.validity =1 ");
	    if (!classId.equals(""))
	    	sql.append(" and b.class_id like '" + classId + "%'");
	    if (!commend.equals(""))
	    	sql.append(" and b.commend=" + commend);
	    if (orderCln.equals(""))
	        sql.append("  order by b." +orderCln +" desc");
	    if (!limitStr.equals(""))
	    	sql.append(" limit " + limitStr );
		List<BuyVo> lnc = jt.query(sql.toString(), new RowMapper<BuyVo>() {
			public BuyVo mapRow(ResultSet rs, int rowNum) throws SQLException {
				BuyVo bv = new BuyVo();
				bv.setClassId(rs.getString("class_id"));
				bv.setBuyId(rs.getString("buying_id"));
				bv.setListRule(rs.getString("list_rule"));
				bv.setStorePath(rs.getString("store_path"));
				bv.setArticleRule(rs.getString("article_rule"));
				bv.setBuyName(rs.getString("buying_name"));
				String smallImg = rs.getString("buy_photo");
				if(smallImg == null || smallImg.trim().length()<1)
					smallImg = defaultLogoImg;
				bv.setImgUrl(smallImg);
				String moneyType = rs.getString("MONEYTYPE");
				if(moneyType!=null&&moneyType.trim().length()>0)
				{
					moneyType = ps.getDataParam("moneytype", moneyType);
				}else
					moneyType = "";
				bv.setMoneyType(moneyType);
				String unit = rs.getString("BUYING_UNIT");
				if(unit!=null&&unit.trim().length()>0)
				{
				  unit = ps.getDataParam("product_unit", unit);
				}else
				  unit = "";
				bv.setUnit(unit);
				bv.setPrice(rs.getDouble("price"));
				bv.setUrl(ClassUtil.getObjUrl(bv, bv.getBuyId()));
				return bv;
			}
		});
		return lnc;
	}

	/**
	 * 获得查询求购信息类别SQL
	 * @param clzLevel
	 * @param clzType
	 * @param page
	 * @param custId
	 * @param commend
	 * @return
	 */
	public String getSqlstr(String clzType, String page, String custId, String commend,String clzLevel)
	  {
	    if (clzType.indexOf("recommended-") > -1)
	    {
	      clzType = clzType.replaceAll("recommended-", "");
	    }
	    String length = "3";
	    int i = 0;
	    if (!clzLevel.equals(""))
	      i = Integer.parseInt(clzLevel);
	    logger.debug("class_level:" + clzLevel);
	    logger.debug("classtype:" + clzType);
	    switch (i)
	    {
	    case 1:
	      length = "3";
	      break;
	    case 2:
	      length = "6";
	      break;
	    case 3:
	      length = "10";
	      break;
	    case 4:
	      length = "15";
	      break;
	    case 5:
	      length = "20";
	      break;
	    default:
	      length = "0";
	    }
	    if (clzType.length() == 0 )
	    	length = "3";
	    else if (clzType.length() == 3)
	    	length = "6";
	    else if (clzType.length() == 6)
	    	length = "10";
	    else if (clzType.length() == 10)
	    	length = "15";
	    else if (clzType.length() == 15)
	    	length = "20";
	 
	    StringBuffer buf = new StringBuffer("select ifnull(p.datacount,0) datacount , bc.class_id,bc.class_name,bc.store_path,bc.home_name,bc.list_rule from buying_class bc " +
	    		" left join (select count(left(class_id,"+length+")) as datacount,left(class_id,"+length+") class_id from buying where  start_date <=now() and end_date >=now() and display=1 and validity =1  ");
	    if (commend.equals("1"))
	    	buf.append(" and commend= 1 ");
	    if (!custId.equals(""))
	    	buf.append(" and cust_id=" + custId);
	    buf.append("  group by left(class_id,"+length+")) as p on p.class_id =bc.class_id  where bc.checked=1 and  bc.remove_tag=1 and bc.class_id like '" + clzType + "%' ");  
	    buf.append(" and length(bc.class_id)=" + length );
	    if (!page.equals(""))
	    	buf.append(" limit " + page);
	  	    
	    logger.debug("求购类别统计："+buf.toString());
	    return buf.toString();
	  }
	
	/**
	 * 生成求购首页
	 * @param context
	 * @return
	 */
	public boolean createIndexHtml(ServletContext context) throws Exception
	{
		boolean flag = false;
		//数据准备
		Map<String,Object> data = new HashMap<String,Object>();
		//生成首页标题
		String webtitle = ps.getParamValue("webtitle");
		data.put("webtitle", "求购--"+webtitle);
		data.put("keywords", Constants.SLOGAN);
		data.put("description", Constants.DESCRIPTION);
		//获取更多求购信息页链接
		ClassVo ncv = getClassById("100");
		String ncvurl = ClassUtil.getListUrl(ncv,1);
		data.put("buymore", ncvurl);
		//获取推荐求购信息列表
		List<BuyVo> recomBuys = queryBuys("100","1","0,6","oper_time");
		data.put("recomBuys", recomBuys);
		//获取推荐企业列表
		List<CompanyVo> lcoms = cs.queryCompanies("","add_date","0,6","1");
		data.put("lcoms", lcoms);
		
		int begin = 0;
		String limitStr = ""+begin+","+Constants.PAGESIZE;
		List<BuyVo> lbv = queryListBuys("100", "", limitStr, "oper_time");
		data.put("lbv", lbv);
		data.put("buynum", Constants.PAGESIZE);
		
		//获取求购类别列表
		List<ClassVo> lcv = queryBuyClzs("100", "", true);
		data.put("buyClzs", lcv);
		ClassVo cv = getClassById("100");
		//获取友情链接列表
		List<LinkVo> llv = ls.queryLinks("100000000000000", "30");
		data.put("frilinks", llv);
		//获取底部信息
		hs.getFooterInfo(data);
		
		hs.crateHTML(context, data, "/"+cv.getIndexModel(), cv.getStorePath()+"/"+cv.getHomeName());
		return flag;
     }
	
	/**
	 * 生成商品的详细内容页
	 * @param buyId
	 * @param context
	 * @return
	 */
	public boolean createContentHtml(String buyId,ServletContext context) throws Exception
	{
		boolean result = false;
		 StringBuffer sql = new StringBuffer("select distinct p.BUYING_ID,p.BUYING_NAME,p.BUYING_UNIT BUYING_UNIT,p.attach_id,a.file_path file_path,p.class_id,p.EXPECT_PRICES price,p.CERTIFY_REQ,p.LOGISTICS, ");
		   sql.append(" p.MONEYTYPE,p.oper_time,p.PUBLISH_USER_ID,p.END_DATE,pc.list_rule,pc.class_name,pc.store_path,pc.article_rule,p.commend,p.RECOMMEND_BUYING,p.list_top,p.PRODUCT_SPEC_REQ spec_req, p.BUYING_NUM,p.BUYING_DESC," );
		   sql.append(" u.qq qq ,u.contact_name contact_name, p.BUYING_ABSTRACT ABSTRACT,pc.Content_Model ,p.BUYING_SITE,p.DELIVERY,c.cust_id cust_id,c.CUST_NAME CUST_NAME,c.JURISTIC JURISTIC,p.PACK_TYPE, c.GROUP_CONTACT_PHONE contact_phone ,c.CELL_PHONE mobile,c.FAX_NBR fax_nbr,c.POST_CODE post_code,c.EMAIL email," );
		   sql.append(" c.CUST_TYPE CUST_TYPE,c.COMPANY_ADDRESS ADDRESS,c.LEVEL LEVEL from buying p join company c on c.cust_id = p.CUST_ID  " );
		   sql.append("  inner join user_info u on u.user_id=p.PUBLISH_USER_ID left join attach a on p.attach_id = a.attach_root_id inner join buying_class pc " );
		   sql.append("  on pc.class_id=p.class_id  where p.buying_id = ?  ");
		   BuyVo pv = jt.queryForObject(sql.toString(),  new RowMapper<BuyVo>() {
			public BuyVo mapRow(ResultSet rs, int rowNum) throws SQLException {
				BuyVo b = new BuyVo();
				b.setClassId(rs.getString("class_id"));
				b.setBuyId(rs.getString("BUYING_ID"));
				b.setListRule(rs.getString("list_rule"));
				b.setClassName(rs.getString("class_name"));
				b.setStorePath(rs.getString("store_path"));
				b.setArticleRule(rs.getString("article_rule"));
				b.setContentModel(rs.getString("Content_Model"));
				b.setBuyName(rs.getString("BUYING_NAME"));
				b.setOperTime(rs.getString("oper_time"));
				String summ = rs.getString("ABSTRACT") != null ?rs.getString("ABSTRACT") : " ";
				b.setSummary(summ);
				String moneyType = rs.getString("MONEYTYPE");
				if(moneyType!=null&&moneyType.trim().length()>0)
				{
					moneyType = ps.getDataParam("moneytype", moneyType);
				}else
					moneyType = "";
				b.setMoneyType(moneyType);
				String unit = rs.getString("BUYING_UNIT");
				unit = ps.getDataParam("product_unit", unit);
				b.setUnit(unit);
				
				b.setDelivery(rs.getString("DELIVERY"));
				b.setPrice(rs.getDouble("price"));
				b.setNumber(rs.getInt("BUYING_NUM"));
				b.setPackType(rs.getString("PACK_TYPE"));
				b.setLogistics(rs.getString("LOGISTICS"));
				b.setDesc(rs.getString("BUYING_DESC"));
				b.setCertifyReq(rs.getString("CERTIFY_REQ"));
				b.setPublishUserid(rs.getString("PUBLISH_USER_ID"));
				
				
				String photo = rs.getString("file_path");
				if(photo == null || photo.trim().length()<1)
					photo = defaultLogoImg;
				b.setImgUrl(photo);
				
				b.setAddr(rs.getString("BUYING_SITE"));
				b.setCommend(rs.getString("commend"));
				b.setUrl(ClassUtil.getObjUrl(b, b.getBuyId()));
				b.setSpecReq(rs.getString("spec_req"));
				b.setEndTime(rs.getString("END_DATE"));
				
				CompanyVo com = new CompanyVo();
				com.setCustId(rs.getString("cust_id"));
				com.setCustName(rs.getString("CUST_NAME"));
				com.setCustType(rs.getString("CUST_TYPE"));
				String officer = rs.getString("JURISTIC")!=null ? rs.getString("JURISTIC"): "";
				com.setOfficer(officer);
				com.setAddress(rs.getString("ADDRESS")!=null ? rs.getString("ADDRESS"): "");
				com.setContactPhone(rs.getString("contact_phone")!=null ? rs.getString("contact_phone"): "");
				com.setMobile(rs.getString("mobile")!=null ? rs.getString("mobile"): "");
				com.setFaxNbr(rs.getString("fax_nbr")!=null ? rs.getString("fax_nbr"): "");
				com.setPostCode(rs.getString("post_code")!=null ? rs.getString("post_code"): "");
				com.setEmail(rs.getString("email")!=null ? rs.getString("email"): "");
				
				String level = rs.getString("LEVEL");
				level = ps.getCustLevelLogo(level);
				com.setLevelLogo(level);
				
				b.setCust(com);
				
				UserInfo ui = new UserInfo();
				ui.setContactName(rs.getString("contact_name"));
				String qq = rs.getString("qq");
				if(qq==null || qq.trim().length()<=0)
					qq = "";
				ui.setQq(qq);
				
				b.setUser(ui);
				return b;
			}},buyId);  
		if(pv!=null)
		{
			//数据准备
			Map<String,Object> data = new HashMap<String,Object>();
			//生成首页标题
			String webtitle = ps.getParamValue("webtitle");
			String title = pv.getClassName()+"-"+pv.getBuyName()+"-"+webtitle;
			data.put("webtitle",  title);
			data.put("keywords", pv.getClassName());
			data.put("description", pv.getSummary());
			//分类链接
			data.put("pathList", getBuyClassUrl(pv.getClassId()));
			//求购详细信息
			data.put("buy", pv);
			List<BuyVo> lpv = queryBuys(pv.getClassId(),"","0,24","oper_time");
			data.put("lpv", lpv);
			//获取底部信息
			hs.getFooterInfo(data);
			
			hs.crateHTML(context, data, "/"+pv.getContentModel(), pv.getUrl());
			result = true;
		}
		return result;
	}
	
	/**
	 * 生成商品(供应)的列表页
	 * @param prodId
	 * @param context
	 * @return
	 */
	public boolean createListHtml(String classId,final int pageNo,ServletContext context) throws Exception
	{
		boolean result = false;
		ClassVo bcv = getClassById(classId);
		bcv.setListUrl(ClassUtil.getListUrl(bcv, pageNo));
		List<ClassVo>  cvls = queryBuyClzs(classId,"0,24",false);
		int begin = (pageNo-1) * Constants.PAGESIZE;
		String limitStr = ""+begin+","+Constants.PAGESIZE;
		List<BuyVo> lpv = queryListBuys(classId, "", limitStr, "oper_time");
		if(bcv!=null && lpv !=null)
		{
			//数据准备
			Map<String,Object> data = new HashMap<String,Object>();
			//生成首页标题
			String webtitle = ps.getParamValue("webtitle");
			String title = bcv.getClassName()+"-"+webtitle;
			data.put("webtitle",  title);
			data.put("keywords", Constants.KEYWORDS);
			data.put("description", Constants.DESCRIPTION);
			//分类导航链接
			data.put("pathList", getBuyClassUrl(bcv.getClassId()));
			//分类链接
			data.put("classList", cvls);
			data.put("lbv", lpv);
			
			List<CompanyVo> coms = cs.queryCompanies("100","add_date","0,3","1");
			data.put("coms", coms);
			//获得当前类别的总的求购信息数
			int num = countBuyNumByClass(bcv.getClassId());
			data.put("totalprod", num);
			int totPage = num%Constants.PAGESIZE==0 ? num/Constants.PAGESIZE : num/Constants.PAGESIZE+1;
			PageTool pt = new PageTool(pageNo,totPage,ClassUtil.getListUrl(bcv));
			data.put("pageBar", pt.getPageBar());
			data.put("pagego", pt.getPreNext());
			//获取底部信息
			hs.getFooterInfo(data);
			
			hs.crateHTML(context, data, "/"+bcv.getListModel(), bcv.getListUrl());
			result = true;
		}
		return result;
	}
	
	/**
	 * 根据条件查询列表页面的求购信息列表
	 * 
	 * @param classId
	 * @param limitStr
	 * @param orderCln
	 * @param commend
	 * @return
	 */
	public List<BuyVo> queryListBuys(String classId,String commend ,String limitStr,
			String orderCln) {
	   StringBuffer sql = new StringBuffer("select distinct p.BUYING_ID,p.BUYING_NAME,p.BUYING_UNIT BUYING_UNIT,p.attach_id,a.file_path file_path,p.class_id, ");
	   sql.append(" p.MONEYTYPE,p.oper_time,pc.list_rule,pc.store_path,pc.article_rule,p.commend,p.RECOMMEND_BUYING,p.list_top,p.PRODUCT_SPEC_REQ spec_req, " );
	   sql.append(" u.qq qq , p.BUYING_ABSTRACT ABSTRACT,p.BUYING_SITE,p.DELIVERY,c.cust_id cust_id,c.CUST_NAME CUST_NAME,c.JURISTIC JURISTIC, " );
	   sql.append(" c.CUST_TYPE CUST_TYPE,c.COMPANY_ADDRESS ADDRESS,c.LEVEL LEVEL from buying p join company c on c.cust_id = p.CUST_ID  " );
	   sql.append("  inner join user_info u on u.user_id=p.PUBLISH_USER_ID left join attach a on p.attach_id = a.attach_root_id inner join buying_class pc " );
	   sql.append("  on pc.class_id=p.class_id where p.START_DATE <=now() and p.END_DATE >=now() and p.display=1 and p.validity =1  ");
	   
	    if (!classId.equals(""))
	    	sql.append(" and p.class_id like '" + classId + "%'");
	    if (!commend.equals(""))
	    	sql.append(" and p.commend=" + commend);
	    if (orderCln.equals(""))
	        sql.append("  order by p." +orderCln +" desc");
	    if (!limitStr.equals(""))
	    	sql.append(" limit " + limitStr );
		List<BuyVo> lnc = jt.query(sql.toString(), new RowMapper<BuyVo>() {
			public BuyVo mapRow(ResultSet rs, int rowNum) throws SQLException {
				BuyVo b = new BuyVo();
				b.setClassId(rs.getString("class_id"));
				b.setBuyId(rs.getString("BUYING_ID"));
				b.setListRule(rs.getString("list_rule"));
				b.setStorePath(rs.getString("store_path"));
				b.setArticleRule(rs.getString("article_rule"));
				b.setBuyName(rs.getString("BUYING_NAME"));
				b.setOperTime(rs.getString("oper_time"));
				String summ = rs.getString("ABSTRACT") != null ?rs.getString("ABSTRACT") : " ";
				b.setSummary(summ);
				String moneyType = rs.getString("MONEYTYPE");
				moneyType = ps.getDataParam("moneytype", moneyType);
				b.setMoneyType(moneyType);
				String unit = rs.getString("BUYING_UNIT");
				if(unit!=null&&unit.trim().length()>0)
				{
				   unit = ps.getDataParam("product_unit", unit);
				   b.setUnit(unit);
				}else{
					 b.setUnit("");
				}
				
				String photo = rs.getString("file_path");
				if(photo == null || photo.trim().length()<1)
					photo = defaultLogoImg;
				b.setImgUrl(photo);
				
				b.setAddr(rs.getString("BUYING_SITE"));
				b.setCommend(rs.getString("commend"));
				b.setUrl(ClassUtil.getObjUrl(b, b.getBuyId()));
				b.setSpecReq(rs.getString("spec_req"));
				
				CompanyVo com = new CompanyVo();
				com.setCustId(rs.getString("cust_id"));
				com.setCustName(rs.getString("CUST_NAME"));
				com.setCustType(rs.getString("CUST_TYPE"));
				com.setOfficer(rs.getString("JURISTIC"));
				com.setAddress(rs.getString("ADDRESS"));
				
				String level = rs.getString("LEVEL");
				level = ps.getCustLevelLogo(level);
				com.setLevelLogo(level);
				
				b.setCust(com);
				
				UserInfo ui = new UserInfo();
				String qq = rs.getString("qq");
				if(qq==null || qq.trim().length()<=0)
					qq = "";
				ui.setQq(qq);
				
				b.setUser(ui);
				return b;
			}
		});
		return lnc;
	}
	
	/**
	 * 根据当前求购类别获取分类导航列表
	 * @param classId
	 * @return
	 */
	public List<ClassVo> getBuyClassUrl(String classId)
	{
		String sql = "select class_name,class_id,list_rule,store_path from buying_class where substr(?,1,3)=class_id or substr(?,1,6)=class_id or substr(?,1,10)=class_id or substr(?,1,15)=class_id order by class_id asc";
		List<ClassVo> lpcs = jt.query(sql,  new RowMapper<ClassVo>() {
			 public ClassVo mapRow(ResultSet rs, int rowNum)
				throws SQLException {
				 ClassVo pcv = new ClassVo();
				 pcv.setClassId(rs.getString("class_id"));
				 pcv.setClassName(rs.getString("class_name"));
				 pcv.setListRule(rs.getString("list_rule"));
				 pcv.setStorePath(rs.getString("store_path"));
				 pcv.setListUrl(ClassUtil.getListUrl(pcv, 1));
				 return pcv;
			}
		},classId,classId,classId,classId);   
		return lpcs;
	}
	
	/**
	 * 统计类别包含的求购信息总数
	 * @param classId
	 * @return
	 */
	public int countBuyNumByClass(String classId)
	{
		String sql = " select count(*) from buying where start_date <=now() and end_date >=now() and display=1 and validity =1 and class_id like ? ";
	    int num = jt.queryForInt(sql,classId+"%");
	    return num;
	}
}
